package org.zxp.guava;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * @program: effectiveJava
 * @description: ${description}
 * @author: X-Pacific zhang
 * @create: 2019-04-28 17:11
 **/
public class TestConsistentHash {
    /**
     * 测试一致性哈希
     */
    @Test
    public void testConsistentHash(){
        List<String> servers = Lists.newArrayList("server1", "server2", "server3", "server4", "server5"
        ,"server11", "server21", "server31", "server41", "server51"
        ,"server31", "server32", "server33", "server34", "server35"
        ,"server51", "server52", "server53", "server54", "server55");
        List<String> userIds = Arrays.asList("zxp123549440","zxp","2222","asdhaksdhsakjdsa","!@#$%^");
        System.out.println("=============目前有"+servers.size()+"个服务器");
        userIds.forEach(userId ->
                    {
                        int bucket = Hashing.consistentHash(Hashing.murmur3_128().hashString(userId, Charset.forName("utf8")), servers.size());
                       System.out.println("userId:"+userId+" bucket:"+bucket);
                    });

        //去掉下标为17的server
        servers.remove(17);
        servers.remove(11);
        servers.remove(10);
        System.out.println("=============下线3台后还有"+servers.size()+"个服务器");
        userIds.forEach(userId ->
        {
            int bucket = Hashing.consistentHash(Hashing.murmur3_128().hashString(userId, Charset.forName("utf8")), servers.size());
            System.out.println("userId:"+userId+" bucket:"+bucket);
        });
    }

    /**
     * 测试经典哈希
     */
    @Test
    public void testClassicHash(){
        List<String> servers = Lists.newArrayList("server1", "server2", "server3", "server4", "server5"
                ,"server11", "server21", "server31", "server41", "server51"
                ,"server31", "server32", "server33", "server34", "server35"
                ,"server51", "server52", "server53", "server54", "server55");
        List<String> userIds = Arrays.asList("zxp123549440","zxp","2222","asdhaksdhsakjdsa","!@#$%^");
        System.out.println("=============目前有"+servers.size()+"个服务器");
        userIds.forEach(userId ->
        {
            long bucket = Math.abs(Hashing.murmur3_128().hashString(userId, Charset.forName("utf8")).padToLong()) % servers.size();
            System.out.println("userId:"+userId+" bucket:"+bucket);
        });
        //去掉下标为17的server
        servers.remove(17);
        servers.remove(11);
        servers.remove(10);
        System.out.println("=============下线3台后还有"+servers.size()+"个服务器");
        userIds.forEach(userId ->
        {
            long bucket = Math.abs(Hashing.murmur3_128().hashString(userId, Charset.forName("utf8")).padToLong()) % servers.size();
            System.out.println("userId:"+userId+" bucket:"+bucket);
        });
    }

}
